﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace Mentor_Database
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void booksBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            try
            {
                this.booksBindingSource.EndEdit();
            }
            catch (NoNullAllowedException ex)
            {
                this.booksBindingSource.CancelEdit();
            }
            this.tableAdapterManager.UpdateAll(this.bookDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bookDataSet.books' table. You can move, or remove it, as needed.
            this.booksTableAdapter.Fill(this.bookDataSet.books);

        }

        private void toolStripTextBox1_TextChanged(object sender, EventArgs e)
        {
            String s;
            ToolStripTextBox tstb = (ToolStripTextBox)sender;
            s = "%" + tstb.Text + "%";
            this.bookDataSet.Clear();
            this.booksTableAdapter.FillBy(this.bookDataSet.books, s);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            Stream xmlStream;
            sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            sfd.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((xmlStream = sfd.OpenFile()) != null)
                    {
                        this.bookDataSet.WriteXml(xmlStream, XmlWriteMode.WriteSchema);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Stream xmlStream = null;
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            ofd.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
            ofd.FilterIndex = 1 ;
            ofd.RestoreDirectory = true ;

            if(ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((xmlStream = ofd.OpenFile()) != null)
                    {
                        DataSet temp = new DataSet();
                        temp.ReadXml(xmlStream, XmlReadMode.Auto);
                        this.bookDataSet.Merge(temp, true, MissingSchemaAction.Ignore);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }
    }
}
